#include "GetMediaThumb.h"
#include <TCHAR.h>
#include <windows.h>
#include <dshow.h>
//#include "dxtrans.h"
#include "amstream.h"
#include <atlbase.h>
#include "qedit.h"

#define CHECKHR(hr) if(FAILED(hr)) return FALSE;

GetMediaThumb::GetMediaThumb(void)
{
}

GetMediaThumb::~GetMediaThumb(void)
{
}

int GetMediaThumb::SaveMediaThumb(wstring MediaPath,wstring ThumbPath)
{
	CComPtr<IMediaDet> pMediaDet;
	pMediaDet.CoCreateInstance(CLSID_MediaDet);
	if (!pMediaDet)
		return FALSE;
	CComBSTR bstrFileName(MediaPath.c_str());
	HRESULT hr = pMediaDet->put_Filename(bstrFileName);
	CHECKHR(hr);
	long StreamNumber = 0;
	hr = pMediaDet->get_OutputStreams(&StreamNumber);
	CHECKHR(hr);	
	bool bHasVideo = false;
	for (long i = 0; i < StreamNumber; i++)
	{
		GUID TypeGuid;
		hr = pMediaDet->put_CurrentStream(i);
		if (FAILED(hr)) 
			continue;
		hr = pMediaDet->get_StreamType(&TypeGuid);
		if (FAILED(hr))
			continue;
		if (TypeGuid == MEDIATYPE_Video)
		{
			bHasVideo = true;
			break;
		}
	}
	if (!bHasVideo)
		return FALSE;
	CComBSTR bstrThuName(ThumbPath.c_str());
	SIZE size;
	size.cx = 64; size.cy = 64;
	hr = pMediaDet->WriteBitmapBits(0, size.cx, size.cy, bstrThuName);
	CHECKHR(hr);

	return TRUE;
}